﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace EFong.Contract
{
    [DataContract]
    public class TrainTicketOrderDetail
    {
        private Guid _id = Guid.NewGuid();
        [DataMember]
        public Guid ID
        {
            get { return _id; }
            set { _id = value; }
        }

        private bool _isToHK = false;
        [DataMember]
        public bool IsToHK
        {
            get { return _isToHK; }
            set { _isToHK = value; }
        }

        public DateTime? DBDepartDate
        {
            set 
            {
                DepartDate = value;
                if(value != null)
                    出发时间 = value.Value.ToString("HHmm");
            }
        }

        private DateTime? _departDate;
        [DataMember]
        public DateTime? DepartDate
        {
            get { return _departDate; }
            set { _departDate = value; }
        }

        private string _出发时间 = string.Empty;
        [DataMember]
        public string 出发时间
        {
            get { return _出发时间; }
            set { _出发时间 = value; }
        }

        private string _departLocation = string.Empty;
        [DataMember]
        public string DepartLocation
        {
            get { return _departLocation; }
            set { _departLocation = value; }
        }

        private string _destination = string.Empty;
        [DataMember]
        public string Destination
        {
            get { return _destination; }
            set { _destination = value; }
        }

        private string trainNo = string.Empty;
        [DataMember]
        public string TrainNo
        {
            get { return trainNo; }
            set { trainNo = value; }
        }

        private string _seatType;
        [DataMember]
        public string SeatType
        {
            get { return _seatType; }
            set { _seatType = value; }
        }

        private decimal? _vendorPrice;
        [DataMember]
        public decimal? VendorPrice
        {
            get { return _vendorPrice; }
            set { _vendorPrice = value; }
        }

        private decimal? _deliveryCost;
        [DataMember]
        public decimal? DeliveryCost
        {
            get { return _deliveryCost; }
            set { _deliveryCost = value; }
        }

        private int _qty;
        [DataMember]
        public int Qty
        {
            get { return _qty; }
            set { _qty = value; }
        }

        private decimal _ticketPrice;
        [DataMember]
        public decimal TicketPrice
        {
            get { return _ticketPrice; }
            set { _ticketPrice = value; }
        }

        //private decimal _totalPrice;
        //[DataMember]
        public decimal? TotalPrice
        {
            get
            {
                if (DeliveryCost == null)
                    return null;

                return (this.TicketPrice + this.DeliveryCost.Value) * Qty;
            }
            //get { return _totalPrice; }
            //set { _totalPrice = value; }
        }

        public decimal? TotalVendorPrice
        {
            get
            {
                if (VendorPrice == null)
                    return null;
                if (trainTicketOrder == null)
                    return null;
                if (trainTicketOrder.Currency != trainTicketOrder.VendorCurrency)
                    if (IsToHK)
                        return VendorPrice.Value * Qty * trainTicketOrder.ExchangeRate;
                    else
                        return (TicketPrice + VendorPrice.Value) * Qty * trainTicketOrder.ExchangeRate;
                else
                    if (IsToHK)
                        return VendorPrice.Value * Qty;
                    else
                        return (TicketPrice + VendorPrice.Value) * Qty;
            }
            //get { return _totalPrice; }
            //set { _totalPrice = value; }
        }


        //private decimal _profit;
        //[DataMember]
        public decimal? Profit
        {
            get
            {
                if (TotalPrice == null)
                    return null;
                if (TotalVendorPrice == null)
                    return null;

                decimal profit = TotalPrice.Value - TotalVendorPrice.Value;
                foreach (TrainTicketReturn trainTicketReturn in this.TrainTicketReturnHis)
                    profit += trainTicketReturn.Profit;

                return profit;
            }
            //get { return _profit; }
            //set { _profit = value; }
        }

        private TrainTicketOrder trainTicketOrder = null;
        public TrainTicketOrder TrainTicketOrder
        {
            get { return trainTicketOrder; }
            set { trainTicketOrder = value; }
        }

        private string status = "使用中";
        [DataMember]
        public string Status
        {
            get { return status; }
            set { status = value; }
        }

        public string 行程
        {
            get
            {
                if (DepartLocation == string.Empty)
                    return string.Empty;
                else
                    return string.Format("{0} - {1}", DepartLocation, Destination);
            }
        }

        private string _日期 = null;
        public string 日期
        {
            get
            {
                if (_日期 == null)
                    if (this.DepartDate == null)
                        return string.Empty;
                    else
                        return this.DepartDate.Value.ToString("yyyy/MM/dd");
                else
                    return _日期;
            }
            set
            {
                _日期 = value;
            }
        }

        public string 时间
        {
            get
            {
                if (_日期 == null)
                    if (this.DepartDate == null)
                        return string.Empty;
                    else
                        return this.DepartDate.Value.ToString("hh:mm");
                else
                    return _日期;
            }
        }

        private List<TrainTicketReturn> _trainTicketReturnHis = new List<TrainTicketReturn>();
        [DataMember]
        public List<TrainTicketReturn> TrainTicketReturnHis
        {
            get { return _trainTicketReturnHis; }
            set { _trainTicketReturnHis = value; }
        }

        public override bool Equals(object obj)
        {
            TrainTicketOrderDetail temp = obj as TrainTicketOrderDetail;
            if (temp == null)
                return false;
            return (this.ID.Equals(temp.ID));
        }
    }
}
